﻿using RFIDData.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RFIDData.Data
{
	public class Dbman
	{
		RFIDDB context = new RFIDDB();
		//string name, bool? sex,string phone, string department, string rank, DateTime hiredate
		public void EmployeeAdd(Employee employee)
		{
			context.Employees.Add(new Employee
			{
				Sex = employee.Sex,
				Name = employee.Name,
				Phone = employee.Phone,
				Department = employee.Department,
				Rank = employee.Rank,
				HireDate = employee.HireDate,
				Image = employee.Image
			});
			context.SaveChanges();
		}
		public List<Employee> EmployeeList()
		{
			return context.Employees.ToList();
		}
		public List<Employee> EmployeeList(Employee employee)
		{
			return context.Employees.Where(
                p => p.Name == employee.Name || p.Rank == employee.Rank || p.Department == employee.Department).ToList();
		}
		
		
        public void CardAdd(Card card)
        {
			
            context.Cards.Add(new Card
            {
                CaNo = card.CaNo,
                Active = card.Active,
                CDate = card.CDate,
                EmNo = card.EmNo
                
            });
            context.SaveChanges();
        }

        public List<Card> CardList()
        {
            return context.Cards.ToList();
        }

        public List<Card> CardList(Card card)
        {
            return context.Cards.Where(c => c.CaNo == card.CaNo).Where(c => c.EmNo == card.EmNo).Where(c=>c.Active==card.Active).ToList();
        }

        public void CommuteAdd(Commute commute)
        {
            context.Commutes.Add(new Commute
            {
                CoNo = commute.CoNo,
                EmNo = commute.EmNo,                
                InTime = commute.InTime,
                OutTime = commute.OutTime,
                Ymd = commute.Ymd
                
            });
            context.SaveChanges();
        }

        private List<CommuteEmployee> _comemp = new List<CommuteEmployee>();

        public List<CommuteEmployee> comemp
        {
            get
            {
                return _comemp;
            }
            set
            {
                _comemp = value;
            }
        }

        public List<CommuteEmployee> CommuteEmployeeList()
        {
            comemp = new List<CommuteEmployee>(context.Commutes.Join(
                context.Employees,
                c => c.EmNo,
                ee => ee.EmNo,
                (c, ee) => new CommuteEmployee
                {
                    CoNo = c.CoNo,
                    InTime = c.InTime,
                    OutTime = c.OutTime,
                    Ymd = c.Ymd,
                    Name = ee.Name,
                    Department = ee.Department,
                    Rank = ee.Rank
                }));
            return comemp;


        }

        public List<CommuteEmployee> CommuteEmployeeList(CommuteEmployee ce)
        {            
            return CommuteEmployeeList()
                .Where(c => c.Name == ce.Name || c.Department == ce.Department || c.Rank == ce.Rank)
                .ToList()
                ;
        }
		public int SearchEmno(string uid)
		{
			MessageBox.Show(uid);
			var kk = context.Cards.Where(p => p.CaNo == uid).Single();
			MessageBox.Show(kk.CaNo);
            return kk.EmNo;

		}

       
	}
}
